package com.riaevolution.components.breadcrumb {
	import flash.display.Graphics;

	import mx.skins.ProgrammaticSkin;

	/**
	 *
	 * <code>
	 * .breadButton {
	 * 		overSkin: ClassReference("BreadCrumbButtonSkin");
	 * 		upSkin: ClassReference("BreadCrumbButtonSkin");
	 * 		downSkin: ClassReference("BreadCrumbButtonSkin");
	 * 		fill-colors:#FF3300,#cc4455;
	 * 		drop-shadow-enabled:true;
	 * }
	 *
	 * <br /><b>Luciano bustos</b> <a href="mailto:luchyx&#64;riaevolution.com">luchyx&#64;riaevolution.com</a><br /><a href="http://www.riaevolution.com">www.riaevolution.com</a>
	 *
	 */
	public class BreadCrumbButtonSkin extends ProgrammaticSkin {

		public var backgroundFillColor:Number = 0xFFFFFF;

		public var lineThickness:Number = 1;

		public var arrowLenght:Number = 8;

		public function BreadCrumbButtonSkin() {
			super();
		}

		override protected function updateDisplayList(w:Number, h:Number):void {
			// Depending on the skin's current name, set values for this skin.
			var colors:Array = getStyle("fillColors");

			if (colors == null || colors.length == 0) {
				colors = [backgroundFillColor, backgroundFillColor];
			}
			var alpha:Number = getStyle("backgroundAlpha");
			var useDropShadow:Boolean = getStyle("dropShadowEnabled");

			if (isNaN(alpha))
				alpha = 1;
			var currentColor:Number = backgroundFillColor;
			var currentLineColor:Number = currentColor;

			switch (name) {
				case "selectedUpSkin":
				case "selectedOverSkin":
				case "upSkin":
					currentColor = colors[0];
					currentLineColor = colors[1];
					break;
				case "overSkin":
					currentLineColor = colors[0];
					currentColor = colors[1];
					break;
				case "downSkin":
				case "selectedDownSkin":
					currentColor = colors[1];
					currentLineColor = currentColor;
					break;
				case "disabledSkin":
					currentLineColor = 0xCCCCCC;
					alpha = .4;
					break;
				case "selectedDisabledSkin":
					currentColor = 0xCCCCCC;
					alpha = .4;
					break;
			}

			var col:Number = getStyle("borderColor");

			if (isNaN(col)) {
				currentLineColor = col;
			}

			var g:Graphics = graphics;
			g.clear();

			if (useDropShadow) {
				g.beginFill(0x222222, .2);
				g.lineStyle(2, 0x222222, .2);
				drawArrow(g, w, h, 2, 2);
			}
			g.beginFill(currentColor, alpha);
			g.lineStyle(lineThickness, currentLineColor);

			drawArrow(g, w, h);
			g.endFill();
		}

		private function drawArrow(g:Graphics, w:Number, h:Number, xOffset:Number = 0, yOffset:Number = 0):void {
			g.moveTo(xOffset, yOffset);
			w = w + xOffset;
			h = h + yOffset;
			g.lineTo(w, yOffset);
			g.lineTo(w + arrowLenght - lineThickness, (h + yOffset) / 2);
			g.lineTo(w, h);
			g.lineTo(xOffset, h);

			if (this.parent && this.parent is BreadCrumbButton) {
				if (BreadCrumbButton(this.parent).firstChild) {
					g.lineTo(xOffset, h / 2);
				} else {
					g.lineTo(arrowLenght + xOffset, h / 2);
				}
			} else {
				g.lineTo(arrowLenght + xOffset, h / 2);
			}
			g.lineTo(xOffset, yOffset);
		}
	}
}